package com.qf.pubsub;

import com.qf.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

/**
 * 自定义交换机
 */
public class Publisher {


    @Test
    public void publish() throws Exception {
        //1. c
        Connection connection = ConnectionUtil.getConnection();

        //2. c
        Channel channel = connection.openChannel().get();

        //3. 声明FANOUT类型交换机，并且让当前交换机绑定队列
        // FANOUT类型交换机需要提前绑定Queue，发送消息时，无论routingKey写什么，FANOUT交换机都会将消息路由到绑定的Queue上。
        channel.exchangeDeclare("pubsub_exchange", BuiltinExchangeType.FANOUT);
        channel.queueBind("pubsub_queue1","pubsub_exchange","3u4iuh5jgj543jihj63j54hih5j");
        channel.queueBind("pubsub_queue2","pubsub_exchange","");

        //4. p
        String msg = "pubsubHeiHeiHei!!";
        channel.basicPublish("pubsub_exchange","asodjihfkjbsdjkfljsdhkjfgsdofusidhjfjsiod",null,msg.getBytes());

        //5. c
        channel.close();
        connection.close();
    }

}
